package me.zhengjie.modules.biz.rest;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.annotation.Log;
import me.zhengjie.exception.BizException;
import me.zhengjie.modules.biz.domain.req.*;
import me.zhengjie.modules.biz.domain.resp.TzRecommendResp;
import me.zhengjie.modules.biz.domain.resp.UniversitiesRecommendResp;
import me.zhengjie.modules.biz.domain.resp.VolunteerFormResp;
import me.zhengjie.modules.biz.domain.vo.VolunteerFormQueryCriteria;
import me.zhengjie.modules.biz.service.*;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static me.zhengjie.modules.biz.constant.GlobalConstant.*;
import static me.zhengjie.modules.biz.domain.enums.ErrorCode.BODY_NOTNULL;
import static me.zhengjie.modules.biz.domain.enums.ErrorCode.NOT_SET_CHANNEL;

/**
 * 通用Controller
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "招生模块：通用(前端使用)")
@RequestMapping("/api/common")
@Slf4j
public class CommonController {

    private final EnrollSingleService enrollSingleService;
    private final CommonService commonService;
    private final UndergraduateService undergraduateService;
    private final VolunteerFormService volunteerFormService;
    private final PrinceCityRegionService princeCityRegionService;
    private final CollegeEntranceExamService collegeEntranceExamService;

    @PostMapping("/recommend")
    @Log("智能荐校")
    @ApiOperation("智能荐校")
    public ResponseEntity<Object> recommendUniversities(@Validated @RequestBody UniversitiesRecommendReq req) throws BizException {
        log.info("/api/common/recommend  智能荐校:{}",req);
        if (req == null || req.getCategory() == null) {
            throw new BizException(BODY_NOTNULL.getCode(),BODY_NOTNULL.getMessage());
        }
        UniversitiesRecommendResp resp = null;
        switch (req.getCategory()) {
            case CATEGORY_GK:
                resp = collegeEntranceExamService.recommendUniversities(req);
                break;
            case CATEGORY_ZSB:
                resp = undergraduateService.recommendUniversities(req);
                break;
            case CATEGORY_DZ:
                resp = enrollSingleService.recommendUniversities(req);
                break;
            default:
                throw new BizException(NOT_SET_CHANNEL.getCode(),NOT_SET_CHANNEL.getMessage());
        }
        return new ResponseEntity<>(resp, HttpStatus.OK);
    }

    @PostMapping("/myVolunteer")
    @Log("我的志愿表")
    @ApiOperation("我的志愿表")
    public ResponseEntity<Object> myVolunteer(@Validated @RequestBody VolunteerFormQueryCriteria req) throws BizException {
        log.info("/api/common/myVolunteer  我的志愿表:{}",req);
        VolunteerFormResp resp = volunteerFormService.myVolunteerForm(req);
        return new ResponseEntity<>(resp, HttpStatus.OK);
    }

    @PostMapping("/addVolunteer")
    @Log("填报志愿")
    @ApiOperation("填报/修改志愿")
    public ResponseEntity<Object> addVolunteer(@Validated @RequestBody VolunteerFormReq req) throws BizException {
        log.info("/api/common/addVolunteer  填报/修改志愿:{}",req);
        Boolean res = volunteerFormService.addVolunteerForm(req);
        return new ResponseEntity<>(res,HttpStatus.OK);
    }


    @PostMapping("/preCheck")
    @Log("智能荐校预检测")
    @ApiOperation("智能荐校预检测")
    public ResponseEntity<Object> preCheck(@RequestBody UniversitiesRecommendReq req) throws BizException {
        log.info("/api/common/preCheck  智能荐校预检测:{}",req);
        return new ResponseEntity<>(commonService.preCheck(req),HttpStatus.OK);
    }

    @PostMapping("/area")
    @Log("三级联动")
    @ApiOperation("三级联动")
    public ResponseEntity<HashMap<String, Map<String, List<String>>>> getArea() {
        log.info("/api/common/area  三级联动");
        return new ResponseEntity<>(princeCityRegionService.getArea(),HttpStatus.OK);
    }

    @PostMapping("/position")
    @Log("获取当前城市名")
    @ApiOperation("获取当前城市名")
    public ResponseEntity<String> position(@Validated @RequestBody PositionReq req) {
        log.info("/api/common/position  获取省份：{}",req);
        return new ResponseEntity<>(princeCityRegionService.position(req),HttpStatus.OK);
    }

    @PostMapping("/prince")
    @Log("四川省-？区")
    @ApiOperation("四川省-？区")
    public ResponseEntity<List<String>> getPrince() {
        log.info("/api/common/prince  获取省份");
        return new ResponseEntity<>(princeCityRegionService.getPrince(),HttpStatus.OK);
    }

    @PostMapping("/recommend/universitiesList")
    @Log("智能荐校院校列表（统招使用）")
    @ApiOperation("智能荐校院校列表（统招使用）")
    public ResponseEntity<TzRecommendResp> universitiesList(@Validated @RequestBody TzRecommendReq req) throws BizException {
        log.info("/api/common/recommend/universitiesList  智能荐校院校列表:{}",req);
        return new ResponseEntity<>(collegeEntranceExamService.universitiesList(req),HttpStatus.OK);
    }

    @GetMapping("/city/{code}")
    @Log("四川省-？市")
    @ApiOperation("四川省-？市")
    public ResponseEntity<List<String>> getCity(@PathVariable Integer code) throws BizException {
        log.info("/api/common/city  获取市级列表");
        return new ResponseEntity<>(princeCityRegionService.getCity(code),HttpStatus.OK);
    }

}
